<?php
include "../include/global.php";
include "../include/admin_check.php";
$xtpl = new XTemplate ("tygiaWrite.htm");
include "setting.php";
//
include "../include/function.php";
$xtpl->assign("left_panel", left_panel());
$xtpl->assign("header_panel", header_panel());
$xtpl->assign("footer_panel", footer_panel());

require_once('../include/class_http.php');
require_once('../include/classSQL.php');
include("class_tygia.php");
//
$xtpl->assign("title", ExchangeRate);

$Date=$_GET['Date'];
$xtpl->assign("Date",$Date);
if($Date){
	$tygia = new Tygia();
	$edit_tygia_ngoaite = "";
	//Doi trang thai
	if(@$_GET["action"]=="change"){
		$tygia->ID=@$_REQUEST["ID"];
		$tygia->ChangeStatus();
	}
	// insert du lieu bang tay
	if(@$_GET["action"]=="go"){
		if(@$_REQUEST["ID"]==""){
			$tygia->Date = date("Y-m-d");
			$tygia->Code = @$_POST["Code"];
			$tygia->Name = @$_POST["Name"];
			$tygia->Buy = @$_POST["Buy"];
			$tygia->Transfer = @$_POST["Transfer"];
			$tygia->Sell  = @$_POST["Sell"];
			$tygia->Status = @$_POST["Status"];
			$tygia->Add();
		}
		else{
			$tygia->GetById(@$_REQUEST["ID"]);
			$tygia->Date = date("Y-m-d");
			$tygia->Code = @$_POST["Code"];
			$tygia->Name = @$_POST["Name"];
			$tygia->Buy = @$_POST["Buy"];
			$tygia->Transfer = @$_POST["Transfer"];
			$tygia->Sell  = @$_POST["Sell"];
			$tygia->Status = @$_POST["Status"];	
			$tygia->Edit();			
		}
	}			
	if(@$_GET["action"]=="delete"){
		$delete_giavang = $tygia->GetById(@$_GET["ID"]);	
		$delete_giavang->Remove();
	}
	if(@$_GET["action"]=="edit"){
		$edit_tygia_ngoaite = $tygia->GetById(@$_GET["ID"]);	
		
	}

	$gv_ar = $tygia->GetByDate($Date);
	if(count($gv_ar)>0){
		foreach($gv_ar as $key=>$value){
		
			$row['ID'] = $value['ID'];
			$row['Code'] = $value['Code'];
			$row['Buy'] = $value['Buy'];
			$row['Sell'] = $value['Sell'];
			
			$xtpl->assign("list",$row);
			$xtpl->parse("MAIN.EDIT.LIST");
		}
	}
	else{
		echo '';
	}
	
	$xtpl->parse("MAIN.EDIT");
}else{	

	$h = new http();
	$h->dir = "bar/";
	
	//$http = 'http://english.vietnamnet.vn/Library/VNN/LibraryVNN1376.js';
	//$table = '<table width=\"100%\" height=\"18\" cellspacing=\"0\" cellpadding=\"2\" bordercolor=\"#999999\" border=\"1\" align=\"center\" style=\"border-collapse: collapse;\"> <tbody> <tr valign=\"middle\" height=\"15\" align=\"left\" class=\"bg_sheet\"> <td width=\"45\" align=\"left\" class=\"left_box2\">';
	
	//$http = 'http://www.msb.com.vn/?set_language=en&cl=en';
	//$table = '<table height="170" cellspacing="1" cellpadding="0" border="0" align="center" width="166"><tbody><tr><td bgcolor="#cccccc" width="52" style="color: rgb(51, 51, 51); text-align: center;">';
	$http = 'http://www.vietcombank.com.vn/exchangerates/';
	$table = '<table class="tbl-01 rateTable" cellspacing="1" border="0" id="ctl00_Content_ExrateView">';
	
	if (!$h->fetch($http)) {
		echo "<h2>There is a problem with the http request!</h2>";
		echo $h->log;
		exit();
	}
	//<table align="center" width="740" cellpadding="0" cellspacing="0" border="0">
	$msft_stats = http::table_into_array($h->body, ($table), 0, null);
	$count = count($msft_stats);
	$vang = $msft_stats;
	if($count >0){
		$box = '<form action="" method="post">
				<input name="txtaction" type="hidden" value="ADD" />';
		for($i=1;$i<$count;$i++){
			if ($vang[$i][0]<>"")
				{
				$box.= '<input type="text" name="Code[]" value="'.$vang[$i][0].'" id="Code" />';
				$box.= '<input type="text" name="Buy[]" value="'.$vang[$i][3].'" id="Buy" />';
				$box.= '<input type="text" name="Sell[]"  value="'.$vang[$i][4].'" id="Sell"  />';
				$box.= '<br>';
			}
		}
		$box.= '<input type="submit" value="Nhập dữ liệu"/>&nbsp;<input type="button" value="Trở lại" onclick="javascript:history.go(-1);" /></form>';
	}
	$xtpl->assign("box", $box);
	 
}

// cap nhat du lieu chep
if(@$_POST["txtaction"]=="ADD"){
	if(count(@$_POST["Code"])<=0){
		echo "hien tai site nguon chua co thong tin";
	}
	else{
		$tygia = new Tygia();	
		$check_exist=count($tygia->GetByDate(date("Y-m-d")));
		for($i=0;$i<count(@$_POST["Code"]);$i++){
			$tygia->Date = date("Y-m-d");
			$tygia->Code = @$_POST["Code"][$i];
			$tygia->Buy = @$_POST["Buy"][$i];
			$tygia->Sell  = @$_POST["Sell"][$i];
			
			if ($check_exist>0)
			{
				
				$tygia->Update();
			}
			else
			{
				$tygia->Add();
			}
		}
		echo "<script language = 'javascript'>
			location.href = 'tygia.php?page=$page';
		</script>";
		exit();
		
	}
}		

//////////////////////////////////////
$xtpl->parse("MAIN");
eval("?".">".$xtpl->text("MAIN"));
?>